﻿using KPIS.GERP.Models.ICM;
using System;
using System.Linq;
using System.Web.Mvc;

namespace KPIS.GERP.WEB.GIMS.Controllers
{
    //// [AccessDeniedAuthorize]
    public class BidderSelectController : Controller
    {
        public ActionResult Index(int BidId)
        {
            var service = new BidService.BidServiceClient();
            var itemService = new PurchaseRequestItemService.PurchaseRequestItemServiceClient();
            var model = service.SelectBidder(new Bidder() { BidId = BidId }, new[] { "MasContact" });
            var bidModel = service.SelectBid(new Bid() { BidId = BidId }, null).FirstOrDefault();
            var itemModel = itemService.Select(new PurchaseRequestItem() { PrId = bidModel.PrId }, new[] { "MasBudgetExpense", "MasResourceItem" });

            ViewBag.ItemModel = itemModel;
            ViewBag.BidId = BidId;

            return View(model);
        }

        public ActionResult Select(int id)
        {
            try
            {
                var service = new BidService.BidServiceClient();
                var model = service.SelectBidderQuotation(new BidderQuotation() { BidderQuoId = id }
                    , new[] { "Bidder.MasContact", "PurchaseRequestItem.MasResourceItem", "PurchaseRequestItem.MasBudgetExpense" }).FirstOrDefault();

                return View(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Select(BidderQuotation model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var service = new BidService.BidServiceClient();
                    var servicePrItem = new PurchaseRequestItemService.PurchaseRequestItemServiceClient();
                    var bidderModelList = service.SelectBidder(new Bidder() { BidId = model.Bidder.BidId }, null);
                    var PrModel = servicePrItem.Select(new PurchaseRequestItem() { PrItemId = model.PrItemId.GetValueOrDefault(-1) }, null).FirstOrDefault();

                    foreach (var bidderModel in bidderModelList)
                    {
                        var quoModelList = service.SelectBidderQuotation(new BidderQuotation() { BidderId = bidderModel.BidderId, PrItemId = model.PrItemId }, null);

                        foreach (var quoModel in quoModelList)
                        {
                            if (quoModel.BidderQuoId == model.BidderQuoId)
                            {
                                quoModel.Selected = "1";//เลือก
                                quoModel.SelectedReason = model.SelectedReason;
                                service.UpdateBidderQuotation(quoModel, Class.GimsMembership.CurrentEmpId);
                                //bidderModel.WinnerStatus = "1";
                                PrModel.RecordStatus = Models.SYS.RecordStatus.ApprovedStatus;
                                servicePrItem.Update(PrModel, Class.GimsMembership.CurrentEmpId);
                            }
                            else
                            {
                                quoModel.Selected = "2";//ไม่เลือก
                                quoModel.SelectedReason = null;
                                service.UpdateBidderQuotation(quoModel, Class.GimsMembership.CurrentEmpId);
                                //bidderModel.WinnerStatus = "2";
                            }

                            //service.UpdateBidder(bidderModel, Class.GimsMembership.CurrentEmpId);
                        }
                    }

                    return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
                }
            }
            catch (Exception ex)
            {
                return Json(new { result = false, message = ex.Message, alertClass = "" });
            }

            return Json(new { result = false, message = KPIS.GERP.Models.Class.ValidateText.RequiredField, alertClass = "" });
        }

        public ActionResult DeSelect(int id)
        {
            try
            {
                var service = new BidService.BidServiceClient();
                var model = service.SelectBidderQuotation(new BidderQuotation() { BidderQuoId = id }, new[] { "Bidder" }).FirstOrDefault();
                var bidderModelList = service.SelectBidder(new Bidder() { BidId = model.Bidder.BidId }, null);

                foreach (var bidderModel in bidderModelList)
                {
                    var quoModelList = service.SelectBidderQuotation(new BidderQuotation() { BidderId = bidderModel.BidderId, PrItemId = model.PrItemId }, null);

                    foreach (var quoModel in quoModelList)
                    {
                        quoModel.Selected = "0";
                        quoModel.SelectedReason = null;
                        service.UpdateBidderQuotation(quoModel, Class.GimsMembership.CurrentEmpId);
                    }
                }

                //foreach (var bidderModel in bidderModelList)
                //{
                //    var quoModelList = service.SelectBidderQuotation(new BidderQuotation() { BidderId = bidderModel.BidderId, PrItemId = model.PrItemId, Selected = "1" }, null);

                //    if (quoModelList.Length > 0)
                //    {
                //        bidderModel.WinnerStatus = "1";
                //    }
                //    else
                //    {
                //        bidderModel.WinnerStatus = "2";
                //    }

                //    service.UpdateBidder(bidderModel, Class.GimsMembership.CurrentEmpId);
                //}

                return Json(new { result = true, message = "บันทึกเรียบร้อยแล้ว", alertClass = "" });
            }
            catch (Exception ex)
            {
                return Json(new { result = false, message = ex.Message, alertClass = "" });
            }
        }

        public ActionResult SendBidResult(int id)
        {
            var service = new BidService.BidServiceClient();
            var bidderModelList = service.SelectBidder(new Bidder() { BidId = id }, null);

            foreach (var bidderModel in bidderModelList)
            {
                var quoModelList = service.SelectBidderQuotation(new BidderQuotation() { BidderId = bidderModel.BidderId, Selected = "1" }, null);

                if (quoModelList.Length > 0)
                {
                    bidderModel.WinnerStatus = "1";
                }
                else
                {
                    bidderModel.WinnerStatus = "2";
                }

                service.UpdateBidder(bidderModel, Class.GimsMembership.CurrentEmpId);
            }

            return RedirectToAction("Index", new { BidId = id });
        }
    }
}
